﻿using Microsoft.VisualBasic;
using Newtonsoft.Json.Linq;
using System.Data;

namespace OnWork.DataCenter.BLL.Standard.Conver
{
    /// <summary>
    /// Json默认解析方法使用 Json
    /// </summary>
    internal class JsonToDataTableDefaultConver : IJsonToDataTableConver
    {
        public DataTable Conver(string jsonStr, JsonToDataTableConfig config)
        {
            try
            {
                DataTable dataTable = new DataTable();

                //创建列
                foreach (var item in config.Cols)
                {
                    DataColumn dc = new DataColumn()
                    {
                        ColumnName = item.ColName,
                        //DataType = typeof(string),
                    };

                    //数据类型
                    switch (item.ColType)
                    {
                        case DataType.String:
                            dc.DataType = typeof(string);
                            break;

                        case DataType.Int32:
                            dc.DataType = typeof(int);
                            break;

                        case DataType.Boolean:
                            dc.DataType = typeof(bool);
                            break;

                        case DataType.DateTime:
                            dc.DataType = typeof(DateTime);
                            break;

                        case DataType.Decimal:
                            dc.DataType = typeof(decimal);
                            break;

                        default:
                            dc.DataType = typeof(string);
                            break;
                    }

                    dataTable.Columns.Add(dc);
                }

                //如果是[开始则是数组判断是数组类型
                if (jsonStr.StartsWith("["))
                {
                    var arr = JArray.Parse(jsonStr);
                    foreach (var item in arr)
                    {
                        //创建行
                        DataRow dr = dataTable.NewRow();

                        for (int i = 0; i < config.Cols.Count; i++)
                        {
                            //获取值
                            string token = config.Cols[i].ColSelectToken;

                            var itemResult = (string)item.SelectToken(token);
                            //赋值到table行
                            dr[i] = itemResult;
                        }
                        dataTable.Rows.Add(dr);
                    }
                }
                else
                {
                    var o = JObject.Parse(jsonStr);

                    //o.SelectTokens(config.dataSelectToken);

                    IEnumerable<JToken> data = o.SelectTokens(config.dataSelectToken);

                    foreach (var item in data)
                    {
                        foreach (var d in item)
                        {
                            //创建行
                            DataRow dr = dataTable.NewRow();

                            for (int i = 0; i < config.Cols.Count; i++)
                            {
                                //获取值
                                string token = config.Cols[i].ColSelectToken;

                                if (token.StartsWith("$")) //如果是$开头则从开始取值
                                {
                                    var itemResult = (string)o.SelectToken(token);
                                    //赋值到table行
                                    dr[i] = CvtDBValue(itemResult, config.Cols[i].ColType);
                                }
                                else
                                {
                                    var itemResult = (string)d.SelectToken(token);
                                    //赋值到table行
                                    dr[i] = CvtDBValue(itemResult, config.Cols[i].ColType);
                                }

                                //if (config.Cols[i].ColType == DataType.Decimal)
                                //{
                                //    if (string.IsNullOrEmpty(dr[i].ToString()))
                                //    {
                                //        dr[i] = DBNull.Value;
                                //    }
                                //}
                            }

                            dataTable.Rows.Add(dr);
                        }
                    }
                }
                return dataTable;
            }
            catch (Exception ex)
            {
                throw;
            }
        }

        /// <summary>
        /// 判断DB值转换
        /// </summary>
        /// <param name="result"></param>
        /// <param name="colType"></param>
        /// <returns></returns>
        private object CvtDBValue(string result, DataType colType)
        {
            if (colType == DataType.Decimal || colType == DataType.Int32)
            {
                if (string.IsNullOrEmpty(result))
                {
                    return DBNull.Value;
                }
            }

            return result;
        }
    }
}